<template>
    <div class="manage">
        <el-dialog
            title="提示"
            :visible.sync="dialogVisible"
            width="50%"
            @open="handleOpen"
            :before-close="handleClose">
            <!-- 项目的表单信息 -->
            <el-form ref="formInfo" :rules="rules" :inline="true" :model="form" label-width="80px">
                <el-form-item label="活动标题" prop="itemTit">
                    <el-input placeholder="请输入标题" v-model="form.itemTit"></el-input>
                </el-form-item>
                <!-- <el-form-item label="学校名称" prop="age">
                    <el-input placeholder="请输入年龄" v-model="form.schoolName"></el-input>
                </el-form-item> -->
                <el-form-item label="服务年限" prop="serviceTime">
                  <el-input placeholder="请输入服务年限" v-model="form.serviceTime"></el-input>
                </el-form-item>
                <el-form-item label="联系人" prop="contacts">
                  <el-input placeholder="请输入联系人" v-model="form.contacts"></el-input>
                </el-form-item>
                <el-form-item label="电话号码" prop="contactsPhone">
                  <el-input placeholder="请输入电话号码" v-model="form.contactsPhone"></el-input>
                </el-form-item>
                <el-form-item label="支教岗位" prop="station">
                  <el-input placeholder="请输入岗位名称" v-model="form.station"></el-input>
                </el-form-item>
                <el-form-item label="人数" prop="itemNum">
                  <el-input placeholder="请输入人数" v-model="form.itemNum"></el-input>
                </el-form-item>
                <el-form-item label="地址" prop="addr">
                    <el-input placeholder="请输入地址" v-model="form.addr"></el-input>
                </el-form-item>
                <el-form-item label="类型" prop="type">
                    <el-input placeholder="请输入支教类型" v-model="form.type"></el-input>
                </el-form-item>
                <el-form-item label="活动描述" prop="desc">
                  <el-input type="textarea" v-model="form.desc"></el-input>
                </el-form-item>
                <el-form-item label="报名时间" prop="activityTime">
                    <el-date-picker
                     v-model="form.activityTime"
                     type="daterange"
                     range-separator="至"
                     start-placeholder="开始日期"
                     end-placeholder="结束日期">
                </el-date-picker>
                </el-form-item>

            </el-form>
            <span>上传项目图片</span>
            <div class="displayImg">

              <div  v-if="form.pic" style="width:250px;heigth:250px"><img style="width:100%;height:100%" :src="evUrl+form.pic" alt="">
              </div>
            <div class="uploadImg">
              <el-upload
              class="itemPic"
              ref="itemPic"
              :action="evUrl+'zj/insertPic'"
              :show-file-list="false"
              :data="itemVal"
              :auto-upload="false"
              :on-change="picPre"
              :on-success="picSuccess"
              :on-error="picError">
              <img v-if="imageUrl" :src="imageUrl" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </div></div>

            <span slot="footer" class="dialog-footer">
                <el-button @click="cancel">取 消</el-button>
                <el-button type="primary" @click="submit">提交</el-button>
            </span>
        </el-dialog>
        <div class="manage-header">
          <div class="header-l">
            <el-button size="medium" @click="handleAdd" type="primary">
                + 新增
            </el-button>
            <el-button size="medium" @click="outputData" type="primary">
                + 导出
            </el-button>
          </div>

            <!-- form搜索区域 -->
            <el-form class="header-form" :inline="true" :model="userForm">
                <el-form-item>
                    <el-input placeholder="请输入活动名称" v-model="userForm.name"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button size="medium" type="primary" @click="onSubmit">查询</el-button>
                </el-form-item>
            </el-form>
        </div>
        <div class="common-tabel">
            <el-table
                stripe
                border
                @selection-change="handleSelectionChange"
                height="100%"
                :row-style="{height:'60px'}"
                :cell-style="{textAlign:'center',padding:'7px'}"
                :header-cell-style="{background:'#87CEFA',color:'#ffffff',textAlign:'center',padding:'7px'}"
                :data="tableData">
                <el-table-column
                 type="selection"
                 width="55">
                </el-table-column>
                <el-table-column
                    prop="item_tit"
                    width="120px"
                    label="活动名称"
                    show-overflow-tooltip>
                </el-table-column>
                <el-table-column
                    prop="item_describe"
                    label="活动描述"
                    show-overflow-tooltip>
                </el-table-column>
                <el-table-column
                    prop="num"
                    width="80px"
                    label="人数"
                    show-overflow-tooltip>
                </el-table-column>
                <el-table-column
                    prop="station"
                    label="岗位名称"
                    show-overflow-tooltip>
                </el-table-column>
                <el-table-column
                    prop="addr"
                    label="地址"
                    show-overflow-tooltip>
                </el-table-column>
                <el-table-column
                    prop="item_type"
                    label="类型"
                    show-overflow-tooltip>
                </el-table-column>
                <el-table-column
                    prop="endTime"
                    width="180"
                    label="截止时间"
                    show-overflow-tooltip>
                    <template slot-scope="scope">
                      <span>{{scope.row.endTime? scope.row.endTime.toString().slice(0,19).replace('T',' '):'否'}}</span>
                    </template>
                </el-table-column>
                <el-table-column
                    prop="exa_status"
                    label="审核状态"
                    width="138">
                    <template slot-scope="scope">
                      <span>{{exaStatus(scope.row.exa_status)}}</span>
                      <el-button style="font-size:18px" type="text" size="mini" icon="el-icon-question" v-if="scope.row.dispass_reason" @click="reasonDetails(scope.row)"></el-button>
                    </template>
                </el-table-column>
                <el-table-column
                    prop="addr"
                    label="操作"
                    width="188">
                    <template slot-scope="scope">
                        <!-- <el-button size="mini" type="info" @click="details(scope.row)">详情</el-button> -->
                        <el-button size="mini" @click="handleEdit(scope.row)">查看</el-button>
                        <el-button type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pager">
                <el-pagination
                    layout="prev, pager, next"
                    :total="total"
                    @current-change="handlePage">
                </el-pagination>
            </div>
        </div>
    </div>
</template>
<script>
import { getPublish, schoolAddPub, schoolUpPub, deletePub } from '../api/record'
export default {
  data () {
    return {
      evUrl: this.$evUrl,
      dialogVisible: false,
      form: {
        itemTit: '',
        serviceTime: '',
        contacts: '',
        contactsPhone: '',
        station: '',
        itemNum: '',
        addr: '',
        desc: '',
        activityTime: '',
        type: '',
        s_id: '',
        pic: ''
      },
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick (picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick (picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick (picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      rules: {
        itemTit: [
          { required: true, message: '请输入项目名称', trigger: 'blur' },
          { min: 4, max: 10, message: '长度在 4 到 10 个字符', trigger: 'blur' }
        ],
        serviceTime: [
          { required: true, message: '请输入服务年限', trigger: 'blur' },
          { min: 1, max: 5, message: '长度在 1 到 5 个字符', trigger: 'blur' }
        ],
        contacts: [
          { required: true, message: '请输入联系人', trigger: 'blur' },
          { min: 2, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur' }
        ],
        contactsPhone: [
          { required: true, message: '请输入联系电话', trigger: 'blur' },
          { min: 11, max: 11, message: '长度在 11 到 11 个字符', trigger: 'blur' }
        ],
        station: [
          { required: true, message: '请输入岗位名称', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        itemNum: [
          { required: true, message: '请输入人数', trigger: 'blur' },
          { min: 1, max: 5, message: '长度在 1 到 5 个字符', trigger: 'blur' }
        ],
        desc: [
          { required: true, message: '请输入项目描述', trigger: 'blur' },
          { min: 5, max: 20, message: '长度在 5 到 20 个字符', trigger: 'blur' }
        ],
        addr: [
          { required: true, message: '请输入地址', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
        ],
        activityTime: [
          { required: true, message: '请输入时间' }
        ],
        type: [
          { required: true, message: '请输入活动类型', trigger: 'blur' },
          { min: 2, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur' }
        ]
      },
      tableData: [],
      modalType: 0, // 0表示新增的弹窗， 1表示编辑
      total: 0, // 当前的总条数
      pageData: {
        page: 1,
        limit: 10
      },
      userForm: {
        name: ''
      },
      imageUrl: '',
      flag: false,
      temp: false,
      itemVal: {},
      multipleSelection: []
    }
  },
  computed: {

  },
  methods: {
    // 查看原因
    reasonDetails (row) {
      this.$alert(row.dispass_reason, '驳回原因', {
        confirmButtonText: '确定'
      })
    },
    // 提交用户表单
    submit () {
      this.$refs.formInfo.validate((valid) => {
        if (valid) {
          // 后续对表单数据的处理
          if (this.modalType === 0) {
            this.form.s_id = window.localStorage.getItem('id')
            schoolAddPub(this.form).then(({ data }) => {
              // console.log(data)
              // console.log(data.itemId)
              this.itemVal.itemId = data.itemId
              this.itemVal.item_pic = ''

              console.log(this.itemVal)
              this.$refs.itemPic.submit()
              // 清空表单的数据
              this.$refs.formInfo.resetFields()
            })
          } else {
            schoolUpPub(this.form).then(() => {
              this.$refs.itemPic.submit()
              this.$refs.formInfo.resetFields()
              this.getList()
            })
          }

          // 关闭弹窗
          this.dialogVisible = false
        }
      })
    },
    exaStatus (status) {
      if (status === '1') {
        return '审核通过'
      } else if (status === '0') {
        return '审核不通过'
      } else {
        return '审核中'
      }
    },
    // 弹窗关闭的时候
    handleClose () {
      this.$refs.formInfo.resetFields()
      this.imageUrl = ''
      this.form.pic = ''
      this.dialogVisible = false
    },
    cancel () {
      this.handleClose()
      // open
    },
    handleEdit (row) {
      this.modalType = 1
      this.dialogVisible = true
      // 注意需要对当前行数据进行深拷贝，否则会出错
      // this.form = JSON.parse(JSON.stringify(row))
      // console.log(row)
      const arr = [row.startTime, row.endTime]
      // console.log(arr)
      this.$nextTick(function () {
        this.form.activityTime = arr
        this.form.itemTit = row.item_tit
        this.form.serviceTime = row.service_time
        this.form.contacts = row.contacts
        this.form.contactsPhone = row.phone
        this.form.station = row.station
        this.form.itemNum = row.num
        this.form.addr = row.addr
        this.form.desc = row.item_describe
        this.form.type = row.item_type
        this.form.pic = row.item_pic
        this.form.itemId = row.itemId
        this.itemVal.itemId = row.itemId
        this.itemVal.item_pic = row.item_pic

        // console.log(row.item_pic)
        // console.log(this.form.pic)
      })

      // this.temp = true
    },
    handleDelete (row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deletePub(row).then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          // 重新获取列表的接口
          this.getList()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleAdd () {
      this.modalType = 0
      this.dialogVisible = true
    },
    // 全选操作
    handleSelectionChange (val) {
      this.multipleSelection = val
    },
    // 导出操作
    outputData () {
      // console.log(this.multipleSelection)
      if (!this.multipleSelection.length > 0) {
        this.$message.error('清选择要导出数据')
        return
      }
      this.$excels.exportExcel('活动列表.xlsx', this.multipleSelection)
      this.$message.success('导出成功')
    },
    // 获取列表的数据
    getList () {
      const token = window.localStorage.getItem('token').slice(6)
      // 获取的列表的数据
      getPublish({ params: { ...this.userForm, ...this.pageData, token } }).then(({ data }) => {
        // console.log(data)
        this.tableData = data.list

        this.total = data.count || 0
      })
    },
    // 选择页码的回调函数
    handlePage (val) {
      // console.log(val, 'val')
      this.pageData.page = val
      this.getList()
    },
    // 列表的查询
    onSubmit () {
      this.getList()
    },
    // 详情列表
    // details (row) {
    //   const detailInfo = `项目名称：${row.itemName}联系人:${row.contacts}联系电话:${row.phone}支教岗位:${row.station}
    //   人数:${row.num},开始时间:${row.startTime}结束时间:${row.endTime}地址：${row.addr}描述:${row.describe}`
    //   this.$alert(detailInfo, '详情', {
    //     confirmButtonText: '确定'
    //     callback: action => {
    //       this.$message({
    //         type: 'info',
    //         message: `action: ${action}`
    //       })
    //     }
    //   })
    // },
    picPre (file, fileList) {
      // this.flag = !this.flag
      this.form.pic = ''
      // console.log(window.URL.createObjectURL(file.raw))
      // console.log(file, fileList)
      this.imageUrl = window.URL.createObjectURL(file.raw)
    },
    picSuccess (response) {
      // this.flag = !this.flag
      // console.log('goodc777777sssssscc')
      // console.log(response)
      this.imageUrl = ''
      this.$refs.itemPic.clearFiles()
      // 重新获取列表的接口
      this.getList()
    },
    picError () {
      // this.flag = !this.flag
      this.imageUrl = ''
      this.$refs.itemPic.clearFiles()
    },
    handleOpen () {
      // console.log('hello')
      this.imageUrl = ''
    }
  },
  mounted () {
    this.getList()
  }
}
</script>
<style lang="less" scoped>
.manage {
    height: 90%;
    .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .displayImg{
    display: flex;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
    .manage-header {
        display: flex;
        height: 45px;
        justify-content: space-between;
        align-items: center;
        .header-form{
          height: 45px;
        }

    }
    .common-tabel {
        position: relative;
        height: 95%;
        .pager {
            position: absolute;
            bottom: -30px;
            right: 20px;
        }
    }
}
</style>
